package com.tiantian.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tiantian.bean.PageBean;
import com.tiantian.dao.AuditRepository;
import com.tiantian.dao.BindingPlatformAccountsRepository;
import com.tiantian.dao.BusinessStoreRepository;
import com.tiantian.dao.BuyersUsersRepository;
import com.tiantian.datajpa.service.impl.GenericServiceImpl;
import com.tiantian.domain.Auditprocessinfo;
import com.tiantian.domain.Bindingplatformaccounts;
import com.tiantian.domain.Businessstore;
import com.tiantian.domain.Buyersusers;
import com.tiantian.ret.Result;
import com.tiantian.service.AuditService;
import com.tiantian.service.BackstagelogService;
import com.tiantian.util.CommonConstant.QueryType;
import com.tiantian.util.ConditionResult;

/**
 * @description 审核管理
 */
@Service
public class AuditServiceImpl extends GenericServiceImpl<Auditprocessinfo, String> implements AuditService {
	private AuditRepository auditRepository;

	@Autowired
	private BuyersUsersRepository buyersUsersRepository;

	@Autowired
	private BindingPlatformAccountsRepository bindingPlatformAccountsRepository;

	@Autowired
	private BusinessStoreRepository businessStoreRepository;
	@Autowired
	private BackstagelogService backstagelogService;

	@Autowired
	public void setAuditRepository(AuditRepository auditRepository) {
		super.setGenericDAO(auditRepository);
		this.auditRepository = auditRepository;
	}

	/**
	 * 审核管理列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 */
	@Override
	public List<Auditprocessinfo> list(Map<String, String> params, PageBean pageBean) {
		ConditionResult conditionResult = getQueryCondition(params, QueryType.LIST);
		List<Auditprocessinfo> ret = auditRepository.queryListBySql(conditionResult.getSql(), pageBean.getStartIndex(), pageBean.getPageSize(),
				conditionResult.getParams(), Auditprocessinfo.class);
		conditionResult = getQueryCondition(params, QueryType.COUNT);
		pageBean.setTotal(auditRepository.queryCountBySql(conditionResult.getSql(), conditionResult.getParams()));
		return ret;
	}

	private ConditionResult getQueryCondition(Map<String, String> searchMap, QueryType type) {
		StringBuilder sql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		switch (type) {
		case LIST:
			sql.append(" select t.* ");

			break;
		case COUNT:
			sql.append(" select count(1)");
			break;
		default:
			break;
		}
		sql.append(" from Auditprocessinfo t where 1=1 ");

		if (searchMap != null) {
			// 审核申请ID
			String auditApplicationId = searchMap.get("auditApplicationId");
			if (StringUtils.isNoneBlank(auditApplicationId)) {
				sql.append(" and auditApplicationId=:auditApplicationId ");
				params.put("auditApplicationId", auditApplicationId.trim());
			}
			// 生财ID
			String userId = searchMap.get("userId");
			if (StringUtils.isNoneBlank(userId)) {
				sql.append(" and userId=:userId ");
				params.put("userId", userId.trim());
			}
			
			// 审核ID类型
			String applicantType = searchMap.get("applicantType");
			if (StringUtils.isNoneBlank(applicantType)) {
				sql.append(" and applicantType=:applicantType ");
				params.put("applicantType", applicantType.trim());
			}
			// 审核申请类型
			String auditTypeId = searchMap.get("auditTypeId");
			if (StringUtils.isNoneBlank(auditTypeId)) {
				sql.append(" and auditTypeId=:auditTypeId ");
				params.put("auditTypeId", auditTypeId.trim());
			}
			// 审核申请状态
			String auditStatus = searchMap.get("auditStatus");
			if (StringUtils.isNoneBlank(auditStatus)) {
				sql.append(" and auditStatus=:auditStatus ");
				params.put("auditStatus", auditStatus.trim());
			}

			// 开始时间
			String starttime = searchMap.get("starttime");
			if (StringUtils.isNoneBlank(starttime)) {
				sql.append(" and SubmitTime>=:starttime ");
				params.put("starttime", starttime.trim());
			}
			// 结束时间
			String endtime = searchMap.get("endtime");
			if (StringUtils.isNoneBlank(endtime)) {
				sql.append(" and SubmitTime<=:endtime ");
				params.put("endtime", endtime.trim());
			}
		}
		if (type == QueryType.LIST) {
			sql.append(" order by t.submitTime desc");
		}
		return new ConditionResult(sql.toString(), params);

	}

	@Override
	public Result<Object> edit(String id, Integer auditStatus, String auditOpinion) {
		Auditprocessinfo auditprocessinfo = this.get(id);
		auditprocessinfo.setAuditStatus(auditStatus);
		auditprocessinfo.setAuditOpinion(auditOpinion);
		this.save(auditprocessinfo);
		// 更改关联信息
		int auditTypeId = auditprocessinfo.getAuditTypeId();
		if (auditTypeId == 1) {// 用户
			Buyersusers buyersusers = buyersUsersRepository.findOne(auditprocessinfo.getAuditAssociationId());
			buyersusers.setIdCardStatus(auditStatus == 2 ? "2" : "3");
			buyersusers.setIdCardAuditNotReason(auditOpinion);
			buyersUsersRepository.save(buyersusers);
			// log
			if (auditStatus == 2) {
				backstagelogService.log("010", id, true);
			} else {
				backstagelogService.log("010", id, false);
			}

		} else if (auditTypeId == 2) {// 买手绑定平台账号审核
			Bindingplatformaccounts bindingPlatformAccounts = bindingPlatformAccountsRepository.findOne(auditprocessinfo.getAuditAssociationId());
			bindingPlatformAccounts.setApprovalStatus(auditStatus == 2 ? '1' : '2');
			bindingPlatformAccountsRepository.save(bindingPlatformAccounts);

			// log
			if (auditStatus == 2) {
				backstagelogService.log("011", id, true);
			} else {
				backstagelogService.log("011", id, false);
			}
		} else if (auditTypeId == 3) {// 商家绑定商铺审核
			Businessstore businessStore = businessStoreRepository.findOne(auditprocessinfo.getAuditAssociationId());
			businessStore.setBindingStatus(auditStatus == 2 ? '2' : '3');
			businessStoreRepository.save(businessStore);

			// log
			if (auditStatus == 2) {
				backstagelogService.log("012", id, true);
			} else {
				backstagelogService.log("012", id, false);
			}
		} else if (auditTypeId == 4) {// 买号审核
			Bindingplatformaccounts bindingPlatformAccounts = bindingPlatformAccountsRepository.getOne(auditprocessinfo.getAuditAssociationId());
			bindingPlatformAccounts.setBuyNoApprovalStatus(auditStatus == 2 ? '2' : '3');
			if (auditStatus == 2) {
				bindingPlatformAccounts.setBuyNoApprovalTime(new Date());
				bindingPlatformAccounts.setBuyNoApprovalEffective('1');
			}
			bindingPlatformAccountsRepository.save(bindingPlatformAccounts);
			// log
			if (auditStatus == 2) {
				backstagelogService.log("013", id, true);
			} else {
				backstagelogService.log("013", id, false);
			}
		}

		return new Result<Object>(true, "提交成功");

	}

	@Override
	public long count(Map<String, String> params) {
		ConditionResult conditionResult = getQueryCondition(params, QueryType.COUNT);
		return auditRepository.queryCountBySql(conditionResult.getSql(), conditionResult.getParams());
	}
}
